package com.carkeeper.caruser.service.impl;

import com.carkeeper.caruser.dao.UserMapper;
import com.carkeeper.caruser.pojo.User;
import com.carkeeper.caruser.service.UserService;
import com.carkeeper.entity.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询所有车主信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<User> PageUser(Integer currentPage, Integer pageSize) {
        //设置默认
        int cuPage=1;//页码
        int pageSi=10;//每页条数

        //判空
        if (currentPage != null && currentPage > 0) {
            cuPage = currentPage;
        }
        if (pageSize != null && pageSize > 0) {
            pageSi = pageSize;
        }

        //分页
        PageHelper.startPage(cuPage, pageSi);
        List<User> users = userMapper.selectAll();
        if (users == null || users.size() <= 0) {
            return null;
        }
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return userPageInfo;
    }

    /**
     * 锁定
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public int updateUserStatus(BigInteger userId,Character status) {
        //要更改数据
        User user = new User();
        user.setStatus(status);
        //锁定目标
        Example example=new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", userId);
        //更改
        int count = userMapper.updateByExampleSelective(user, example);
        return count;
    }

    /**
     * 多条件查询
     * @param map
     * @return
     */
    @Override
    public PageInfo<User> searchUser(Map map) {

        //判空
        if (map == null || map.size() == 0) {
            PageInfo<User> userPageResult = PageUser(1, 10);
            return userPageResult;
        }
        PageHelper.startPage(1,10);
        Example example = createExample(map);//自定义方法
        List<User> users = userMapper.selectByExample(example);
        PageInfo info = new PageInfo(users);
        return info;

    }

    /**
     * 根据id查询车主
     * @param userId
     * @return
     */
    @Override
    public User findUserById(String userId) {
        BigInteger id = new BigInteger(userId, 10);
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    /**
     * 构建查询对象
     * @param
     * @return
     */
    private Example createExample(Map map){
        Example example=new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if(map.get("username")!=null && !"".equals(map.get("username"))){
            criteria.andEqualTo("username", map.get("username"));//用户名
        }
        if(map.get("phone")!=null && !"".equals(map.get("phone"))){
            criteria.andEqualTo("phone", map.get("phone"));//电话
        }
        if(map.get("status")!=null && !"".equals(map.get("status"))){
            criteria.andEqualTo("status", map.get("status"));//状态
        }
        return example;
    }
}
